﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using MyLib;
 

namespace PerformanceReport
{
    public partial class ProcessAvg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBindProcess();
                //BindData();               
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true); 
            }
        }

        //绑定下拉框
        public void DataBindProcess()
        {
            DataTable dtProcess;
            string strSql = "select Distinct ProcessName from PROCESSES ";
            dtProcess = DataAccess.Instance("UltDB").ExecuteDataTable(strSql.ToString());
            drpBusinessUnit.DataSource = dtProcess;

            drpBusinessUnit.DataTextField = "ProcessName";

            drpBusinessUnit.DataValueField = "ProcessName";

            drpBusinessUnit.DataBind();

            drpBusinessUnit.Items.Insert(0, new ListItem("--请选择--", "0"));

        }

        private void BindData()
        {
            try
            {
                DataTable dt;
                int PageIndex = AspNetPager1.CurrentPageIndex - 1;
                int PageSize = AspNetPager1.PageSize;


                string strSql = " Select max(ProcessVersion) as Ver ,ProcessName, "         //得到最大的版本号（即最新版本号）
                    //+"ROUND(avg(ROUND(to_date(endtime) - to_date(starttime)))* 24 ,2) as Hours  "
                              + "avg(datediff(hour,starttime,endtime)) as Hours "
                              + "  from Incidents";
                string where = " where 1=1 ";
                where += " and ProcessName like N'" + drpBusinessUnit.SelectedValue.Trim() + "%'";
                if (!string.IsNullOrEmpty(txtStart.Text))
                {
                    where += " and  starttime >= '" + txtStart.Text.Trim() + "'";
                }
                if (!string.IsNullOrEmpty(txtEnd.Text))
                {
                    where += " and  starttime <= '" + txtEnd.Text.Trim() + "'";
                }
                where += "  group by ProcessName  order by ProcessName";
                string StrSqlTotal = strSql + where;
                dt = DataAccess.Instance("UltDB").ExecuteDataTable(StrSqlTotal.ToString());

                //初始化分页数据源实例
                PagedDataSource pds = new PagedDataSource();

                //设置总行数
                AspNetPager1.RecordCount = dt.Rows.Count;
                //设置分页的数据源
                pds.DataSource = dt.DefaultView;
                //设置当前页
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                //设置每页显示页数
                pds.PageSize = AspNetPager1.PageSize;
                //启用分页
                pds.AllowPaging = true;

                //设置Repeater的数据源为分页数据源
                rpSource.DataSource = pds;
                rpSource.DataBind();
                lbTotal.Text = "共" + dt.Rows.Count.ToString() + "条记录";

            }
            catch (Exception ex)
            {
                MessageBox("alert('" + ex.Message + "');");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //AspNetPager1.CurrentPageIndex = e.;

            BindData();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true); 

        }

        //平均消耗（天）
        public string GetAvgDay(string strHour)
        {
            string strAvgDay = null;
            if (strHour != "")
            {
                double dDay = double.Parse(strHour);
                double dAvgDay = Math.Round(dDay/24, 0, MidpointRounding.AwayFromZero);
                strAvgDay = dAvgDay.ToString();
            }
            return strAvgDay;
            
        }

        // 平均消耗（小时）
        public string GetAvgHours(string strHour)
        {
            string strHorus = null;          
            if (strHour != "")
            {
                double dHour = double.Parse(strHour);
                double dAvgHour = Math.Round(dHour, 0, MidpointRounding.AwayFromZero);
                strHorus = dAvgHour.ToString();
            }
            return strHorus;
        }

        private void MessageBox(string script)
        {
            this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "javascript", "<script language='javascript' defer>" + script + "</script>");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true); 
            
        }
    }
}